<?php
 	namespace app\admin\controller;

 	class Category extends Action{
 		public function index(){ 		
 			return view();
 		}

 		public function lists(){
	        $pagesize = input('pagesize');
	        $pages = input('pages');
	        if($pages<=1)
	            $a = 0;
	        else
	            $a = $pagesize*($pages-1);
	        $where = "cid=0";
	        $total = db("type")->where($where)->count();
	        $totalpage = ceil($total/$pagesize);
	        $data = db("type")->where($where)->order("vieworder","asc")->limit($a,$pagesize)->select();
	        //输出Json数据
	    	return json(['code'=>1,'message'=>"获取数据成功",'data'=>$data,'totalpage'=>$totalpage]);
 		}

 		public function add(){
 			if(input("act")=="add"){
 				$data = input("data/a");
 				$save = db("type")->insert($data);
 				if($save){
	                return 1;
	            }else{
	                return 0;
	            }
 			}
 			return view();
 		}

 		public function edit(){ 
 			$id = input("id");
 			if(input("act")=="edit"){
 				$data = input("data/a");
 				$update = db("type")->where('id',"=",$id)->update($data);
 				if($update!==false){
	                return 1;
	            }else{
	                return 0;
	            }
 			}
 			$data = db("type")->where('id',$id)->find();
 			$this->assign([
 				'data'=>$data,
 			]);
 			return view();
 		}

 		public function delete(){
 			$id=input('id');
 			$del = db("type")->where("id","=",$id)->whereOr("cid","=",$id)->delete();
			if($del)
		      return 1;
		    else
		      return 0;
 		}

 		//子类列表开始

 		public function smallIndex(){ 			

 			$cid = input("cid");

 			return $this->fetch('smallindex',['cid'=>$cid]);

 		}

 		public function addsmall(){

 			if(request()->isPost()){

 				$data["name"] = input('name');

 				$data["cid"] = input('cid');

 				$data["vieworder"] = input('vieworder');

 				$data["tidai"] = input('tidai');

 				if(request()->file('imgpath')){

	                $data["imgpath"] = model('Upload')->aUpload(request()->file('imgpath'));

	            }
	            if(request()->file('imgpath2')){

	                $data["imgpath2"] = model('Upload')->aUpload(request()->file('imgpath2'));

	            }

	            $save=db("type")->insert($data);

 				if($save){

 					return backUrl();

 				}else{

 					return backUrl();

 				}

 			}

 			$cid = input('cid');

 			return $this->fetch('addsmall',["cid"=>$cid]);

 		}

 		public function smalllists(){

 			$cid = input('cid');

 			$where = "a.cid=$cid";

	        $pagesize = input('pagesize');

	        $pages = input('pages');

	        if($pages<=1)

	            $a = 0;

	        else

	            $a = $pagesize*($pages-1);

	        $total = db("type")->alias("a")->join("type b","a.cid=b.id")->where($where)->count();

	        $totalpage = ceil($total/$pagesize);

	        $data = db("type")->alias("a")

	       			->join("type b","a.cid=b.id")

	        		->field('a.name,a.id,b.name as cname,a.vieworder')

			        ->where($where)

			        ->order("a.vieworder asc")

			        ->limit($a,$pagesize)

			        ->select();

	        //输出Json数据

	    	return json(['code'=>1,'message'=>"获取数据成功",'data'=>$data,'totalpage'=>$totalpage]);

 		}

 		public function editsmall(){

 			$id = input("id");

 			if(input('name')){

 				$data["name"] = input('name');

 				$data["cid"] = input('cid');

 				$fenlei = input('fenlei');

 				$data["vieworder"] = input('vieworder');

 				$data["tidai"] = input('tidai');

 				if(request()->file('imgpath')){

                    if(is_file("./".input('imgtext'))) @unlink("./".input('imgtext'));

                    $data["imgpath"] = model('Upload')->aUpload(request()->file('imgpath'));

                }else{

                    $data["imgpath"] = input('imgtext');

                }
                if(request()->file('imgpath2')){

                    if(is_file("./".input('imgtext2'))) @unlink("./".input('imgtext2'));

                    $data["imgpath2"] = model('Upload')->aUpload(request()->file('imgpath2'));

                }else{

                    $data["imgpath2"] = input('imgtext2');

                }

                $update = db("type")->where('id',$id)->update($data);

                if($update!==false) return backUrl();                

 			}

 			$data = db("type")->where('id',$id)->find();

 			return $this->fetch('editsmall',['data'=>$data]);

 		}

 		public function deletesmall(){

 			$del = db("type")->where("id","=",input("id"))->delete();

	        if($del)

	            return 1;

	        else

	            return 0;

 		}

 		public function getType(){
	      $cid = input("cid");
	      $typeRes = db("type")->where("cid","=",$cid)->select();
	      return json(["data"=>$typeRes]);
	    }  

 	}